package com.jackchong.ext

/**
 * author : chong.huang
* create : 2021/6/3
 * desc   :
 */

fun  <T> Iterable<T>.find(formIndex: Int = 0, toInex: Int = this.count(), predicate: (T) -> Boolean): T? {
    for ((index, bean) in this.withIndex()) {
        if (index < formIndex || index > toInex) continue
        if (predicate(bean)) {
            return bean
        }
    }
    return null
}

fun  <T> Iterable<T>.findLast(formIndex: Int = 0, toInex: Int = this.count(), predicate: (T) -> Boolean): T? {
    var last: T? = null
    for ((index, bean) in this.withIndex()) {
        if (index < formIndex || index > toInex) continue
        if (predicate(bean)) {
            last = bean
        }
    }
    return last
}
